<?php

namespace App\Models\Product;

// 引入模块
use Illuminate\Database\Eloquent\Model;
use App\Traits\SerializeDate;

/**
 * 订单产品数据模型
 */
class OrderProduct extends Model
{
    use SerializeDate;
    // 设置当前模型对应的完整数据表名称
    protected $table = 'pre_order_product';

    protected $dateFormat = 'U';

    // 允许批量赋值
    protected $guarded = [];

    // 定义创建时间的字段
    const CREATED_AT = null;

    const UPDATED_AT = 'updatetime';

    const DELETED_AT = null;

    protected $appends = [
        'proimg_text', // 多张图片
    ];

    protected $casts = [
        'updatetime'   => 'datetime:Y-m-d H:i:s'
    ];


    public function getProimgTextAttribute($value)
    {
        $ConfigModel = new \App\Models\Config();
        $http = $ConfigModel->where('name', 'url')->value('value');
        $thumbs = '';
        $thumbslist = empty($this->attributes['proimg']) ?  '' :  $this->attributes['proimg'];
        if (!empty($thumbslist)) {
            // 字符串替换
            $str =  str_replace("/uploads/", "$http/uploads/", $thumbslist);
            $thumbs = explode(',', $str);
        } else {
            // 返回默认值
            $thumbs = [];
        }
        return $thumbs;
    }


    // 商品
    public function product()
    {
        return  $this->belongsTo('App\Models\Product\Product', 'proid', 'id');
    }

    // 用户的模型
    public function business()
    {
        return  $this->belongsTo('App\Models\Business\Business', 'busid', 'id');
    }
}
